package eteam.aps.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import eteam.aps.api.U8Api;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.constant.QCAcceptType;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Assert;
import eteam.aps.common.helper.Helper;
import eteam.aps.mapper.ArrivalVouchMapper;
import eteam.aps.mapper.ProductConsumerMapper;
import eteam.aps.model.dto.ProductConsumerDto;
import eteam.aps.model.entity.arrival.ArrivalVouch;
import eteam.aps.model.entity.ProductConsumer;
import eteam.aps.model.entity.other.OutBound;
import eteam.aps.model.vo.arrival.ArrivalVouchShowVo;
import eteam.aps.service.ProductConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产领料 业务层实现类
 *
 * @author LiuLei
 */
@Slf4j
@Service
public class ProductConsumerServiceImpl implements ProductConsumerService {

    @Autowired
    private ArrivalVouchMapper arrivalVouchMapper;

    @Autowired
    private ProductConsumerMapper productConsumerMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Override
    public List<OutBound> getOtherOutbound(String scan) throws Exception {
        return U8Api.getOtherOutbound(scan);
    }

    @Override
    @Transactional
    public String addBound(Map<String, Object> map) throws Exception {
        JSONObject u8 = U8Api.formatTime(map);
        JSONArray ds = u8.getJSONArray("data");
        // 查询库存对于的时间信息
        U8Api.setAccessTimes(ds);
//        System.err.println("出库单：" + ds);
        // todo 出库单测试环境先注释调
        String pro = U8Api.ptsProOut(ds.toString());
//        String pro = null;
        if (Helper.isNotEmpty(pro)) {
            List<ProductConsumer> list = new ArrayList<>();
            for (Object data : u8.getJSONArray("data")) {
                JSONObject head = (JSONObject) data;
                for (Object rows : head.getJSONArray("Rows")) {
                    JSONObject row = (JSONObject) rows;
                    list.add(new ProductConsumer(row.getInt("iMoseq"), row.getStr("cInvCode"), row.getStr("cInvName"), row.getStr("cInvStd"), row.getStr("unit"), row.getStr("alreadyQty"), row.getStr("barCode"), row.getStr("cBatch")));
                }
            }
            String fuserid = loginUserRedisService.getUser().getFuserid();
            list.forEach(data -> Helper.setDefaultAddValue(data, fuserid));
            // 保存
            if (!list.isEmpty()) productConsumerMapper.batchProduceConsumer(list);
        }
        return pro;
    }

    @Override
    public ArrivalVouch getOneByPurBarCode(String scan) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("type", QCAcceptType.ALLOWED);
        if (Helper.isNotEmpty(scan)) map.put("bar", scan);
        return arrivalVouchMapper.getOneByProConsumerBarCode(map);
    }

    @Override
    public BigDecimal getCurrentStock(ProductConsumerDto productConsumerDto) throws Exception {
        Map<String, Object> map = new HashMap<>(5);
        if (Helper.isNotEmpty(productConsumerDto.getWhCode())) map.put("whcode_begin", productConsumerDto.getWhCode());
        if (Helper.isNotEmpty(productConsumerDto.getWhCode())) map.put("whcode_end", productConsumerDto.getWhCode());
        if (Helper.isNotEmpty(productConsumerDto.getInvCode()))
            map.put("invcode_begin", productConsumerDto.getInvCode());
        if (Helper.isNotEmpty(productConsumerDto.getInvCode())) map.put("invcode_end", productConsumerDto.getInvCode());
        if (Helper.isNotEmpty(productConsumerDto.getBatch())) map.put("batch", productConsumerDto.getBatch());
        return U8Api.getCurrentStock(map);
    }

    @Override
    public Map<?, ?> getStockByBarCode(String barCode) throws JsonProcessingException {
        Map<String, Object> map = new HashMap<>(3);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("bar", barCode);
        map.put("type", QCAcceptType.STORAGE);
        ArrivalVouchShowVo one = arrivalVouchMapper.getOneByBar(map);
        Assert.isEmpty(one, "暂无领料数据！");
        Map<String, Object> stock = U8Api.getStockByInvCodeAndBatch(one.getFclnvCode(),
                one.getfCBatch());
        stock.put("cFree1", one.getfBanbencode());
        stock.put("cDate", one.getfDDate());
        return stock;
    }
}
